function compressImage(imageFile, maxWidth, maxHeight, quality = 0.8) {
    return new Promise((resolve, reject) => {
        const image = new Image();
        image.onload = () => {
            const canvas = document.createElement('canvas');
            const ctx = canvas.getContext('2d');

            // 计算新的尺寸以保持图片比例
            let width = image.width;
            let height = image.height;

            if (width > height) {
                if (width > maxWidth) {
                    height *= maxWidth / width;
                    width = maxWidth;
                }
            } else {
                if (height > maxHeight) {
                    width *= maxHeight / height;
                    height = maxHeight;
                }
            }

            canvas.width = width;
            canvas.height = height;

            // 绘制图片到canvas上
            ctx.drawImage(image, 0, 0, width, height);

            // 将canvas内容转换为Blob对象
            canvas.toBlob((blob) => {
                if (!blob) {
                    reject(new Error('Could not compress the image.'));
                    return;
                }
                // 压缩完成，返回blob对象
                resolve(blob);
            }, 'image/jpeg', quality);
        };

        image.onerror = (error) => {
            reject(error);
        };

        // 设置图片源为File对象，开始加载图片
        const reader = new FileReader();
        reader.readAsDataURL(imageFile);
        reader.onloadend = function() {
            image.src = reader.result;
        };
    });
}